
public class DiscretePerceptron {

	private double[] weightings;
	private double learningRate;
	
	public void setWeightings(double[] w) {
		weightings = w;
	}
	
	public void setLearningRate(double c) {
		learningRate = c;
	}
	
	public double[] getWeightings() {
		return weightings;
	}
	
	//size of input == size of weightings 
	public double calcOutput(double[] y) {
		//using the sgn activation fn
		// return +1 if v > 0; -1 if v < 0
		// v = input' * weightings 
		double sum = 0.00;
		int j = 0;
		for (double i : y) {
			sum += i * weightings[j];
			j++;
		}
		//now calculate the output 
		int z = 0;
		if (sum > 0)
			z = 1;
		else
			z = -1;
		//don't forget to update the weightings 
		return z;
	}
	
	public void updateWeightings(double[] i, double z, double d) {
		double newWeights[] = new double[weightings.length];
		//w* = w + 0.5c[d- sng(w'y)]y
		int j = 0;
		for (double nw : newWeights) {
			nw = weightings[j] + (0.5*learningRate)*(d - z)*i[j];
			weightings[j] = nw;
			//System.out.println("New weight["+j+"] is: " + nw);
			j++;
		}
	}
	
}
